<?php
include("include/config.inc.php");

$pageName = 'storeRate';

if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:index.php");
}
else
{
	$partyArray = 0;
	$matArray = 0;

	if(isset($_POST['storeRateBtn']))
	{
    $delPresentRecord = "DELETE FROM fixrate WHERE partyId = ".$_POST['insPartyId'];
    $delPresentRecordRes = mysql_query($delPresentRecord);
		foreach($_POST['rate'] AS $rateKey => $rateValue)
		{
		  $insertRate = "INSERT INTO fixrate(partyId,finalProcessId,rate)
		                 VALUES (".$_POST['insPartyId'].",".$rateKey.",".$rateValue.")";
      $insertRateRes = mysql_query($insertRate);
		}
	}

	if(isset($_POST['showData']))
	{
		$partyArray = array();
		if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
		{
		  $selectParty = "SELECT partyId, partyName
		                    FROM party
		                   WHERE partyId = ".$_POST['partyId']."
                       ORDER BY partyName";
      $selectPartyRes = mysql_query($selectParty);
      while($pRow = mysql_fetch_array($selectPartyRes))
      {
      	$partyArray['partyId']   = $pRow['partyId'];
      	$partyArray['partyName'] = $pRow['partyName'];
      }
    }
    else if (isset($_POST['partyId']) && $_POST['partyId'] == 0)
    {
      $partyArray['partyId']   = 0;
      $partyArray['partyName']   = 'All';
    }
    
    $selMaterial = "SELECT finalProcessId,finalProcessName
	                     FROM finalprocess";
    if(isset($_POST['finalProcessId']) && $_POST['finalProcessId'] > 0)
    {
      $selMaterial .= " WHERE finalProcessId = ".$_POST['finalProcessId']." ";
    }
    $selMaterial .= " ORDER BY finalProcessName";
    $selMaterialRes = mysql_query($selMaterial);
    $matArray = array();
    $matArray[0]['finalProcessId']   = 0;
    $matArray[0]['finalProcessName'] = "All";
    $c = 1;
    while($mRow = mysql_fetch_array($selMaterialRes))
    {
    	$matArray[$c]['finalProcessId']   = $mRow['finalProcessId'];
    	$matArray[$c]['finalProcessName'] = $mRow['finalProcessName'];
    	$c++;
    }
	}
	$k = 0;
	$select = "SELECT partyId,partyName
	             FROM party
	            ORDER BY partyName";
  $selectResult = mysql_query($select);
	while($row = mysql_fetch_array($selectResult))
	{
	  $catTitleArray['partyId'][$k]   = $row['partyId'];
	  $catTitleArray['partyName'][$k] = $row['partyName'];
	  $k++;
	}
	$a = 0;
	$selectMaterial = "SELECT finalProcessId,finalProcessName
	                     FROM finalprocess
	                    ORDER BY finalProcessName";
	$selectMaterialResult = mysql_query($selectMaterial);
	while($row = mysql_fetch_array($selectMaterialResult))
	{
	  $materialArray['finalProcessId'][$a]   = $row['finalProcessId'];
	  $materialArray['finalProcessName'][$a] = $row['finalProcessName'];
	  $a++;
	}

}
include("./bottom.php");
$smarty->assign("catTitleArray",$catTitleArray);
$smarty->assign("materialArray",$materialArray);
$smarty->assign("partyArray",$partyArray);
$smarty->assign("matArray",$matArray);
$smarty->display('storeRate.tpl');
?>